﻿/*
 *  Author: Louis A. Leskoviansky
 *  Company: Artemis Solutions Group (http://www.artemis-solutions.com)
 */


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace AjaxControlFramework
{
    public static class AjaxControlUtils
    {
        //------// Properties \\--------------------------------------------\\

        //------\\ Properties //--------------------------------------------//



        //------// Methods \\-----------------------------------------------\\
        public static List<HtmlControl> FindChildControlsByTagName(Control parent, string tagName)
        {
            List<HtmlControl> controls = new List<HtmlControl>();
            FindChildControlsByTagName(parent, tagName.ToLower(), ref controls);

            return controls;
        }


        private static void FindChildControlsByTagName(Control parent, string tagName, ref List<HtmlControl> controls)
        {
            if (parent.Controls.Count == 0) { return; }

            foreach (HtmlControl childControl in parent.Controls.OfType<HtmlControl>())
            {
                if (childControl.TagName.ToLower() == tagName)
                {
                    controls.Add(childControl);
                }

                FindChildControlsByTagName(childControl, tagName, ref controls);
            }
        }


        public static T FindParentControlByType<T>(Control child) where T : class
        {
            T parentControl = default(T);
            FindParentControlByType<T>(child, ref parentControl);

            return parentControl;
        }


        private static void FindParentControlByType<T>(Control child, ref T parentControl) where T : class
        {
            Control parent = child.Parent;

            if (parent == null)
            {
                parentControl = default(T);
            }
            else
            {
                if (parent is T)
                {
                    parentControl = parent as T;
                }
                else
                {
                    FindParentControlByType<T>(parent, ref parentControl);
                }
            }
        }


        public static List<T> FindChildControlsByType<T>(Control parent) where T : class
        {
            List<T> controls = new List<T>();
            FindChildControlsByType<T>(parent, ref controls);

            return controls;
        }


        private static void FindChildControlsByType<T>(Control parent, ref List<T> controls) where T : class
        {
            controls.AddRange(parent.Controls.OfType<T>());

            foreach (Control childControl in parent.Controls)
            {
                FindChildControlsByType<T>(childControl, ref controls);
            }
        }
        //------\\ Methods //-----------------------------------------------//
    }
}
